<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        #app {
            box-sizing: border-box;
            padding: 10px;
        }

        .abox input {
            padding-left: 15px;
            width: 75%;
            height: 35px;
            outline: none;
            border: 1px solid gray;
        }

        .abox button {
            width: 50px;
            color: green;
            font-weight: 700;
            border: none;
            outline: none;
            text-align: center;
            line-height: 35px;
            background-color: transparent;
        }

        .taskbox .item {
            height: 50px;
            background-color: red;
            color: white;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-sizing: border-box;
            padding: 0 15px;
            margin-top: 20px;
        }

        .taskbox .item div {
            margin-left: -100px;

        }

        .taskbox h4 {
            padding: 0 5px;
        }

        .taskbox .item.finished {
            background-color: rgb(82, 243, 130);
        }

        .taskbox .item button {
            width: 50px;
            color: rgb(255, 255, 255);
            border: none;
            outline: none;
            text-align: center;
            line-height: 35px;
            background-color: transparent;
        }

        .total {
            display: flex;
            margin-top: 20px;
            margin-bottom: 20px;
            justify-content: space-around;
        }

        .total span {
            display: inline-block;
            color: white;
            padding: 5px 10px;
            background-color: rgb(82, 243, 130);
        }

        .total span.undo {
            background-color: red;
        }

        .empty {
            font-size: 12px;
            text-align: center;
            line-height: 50px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="abox">
            <input type="text" placeholder="输入任务名称" v-model="inputText">
            <span @click="add">新增</span>
        </div>
        <div class="total">
            <span>全部任务({{todoList.length}})</span>
            <span>已完成({{finishTask()}})</span>
            <span class="undo">未完成({{undoTask()}})</span>
        </div>
        <div class="taskbox">
            <h4>任务列表</h4>
            <div v-bind:class="`item ${item.status==true?'finished':''}`" v-for="(item ,index) in todoList"
                v-bind:key="index">
                <input type="checkbox" v-on:click="changeStatus(item)">
                <div>{{item.name}}</div>
                <button @click="removeTask(index)">删除</button>
            </div>
            <div class="empty" v-show="todoList.length == 0">任务列表空空如也!</div>
        </div>
</body>

</html>
<script src="../js/vue-2.5.21.js"></script>
<script>
    var v = new Vue({
        el: '#app',
        data: {
            inputText: '',
            todoList: []
        },
        methods: {
            add() {
                if (this.inputText) {
                    var obj = {
                        id: this.todoList.length + 1,
                        name: this.inputText,
                        status: false
                    }
                    this.todoList.push(obj);
                    this.inputText = '';
                }
            },
            changeStatus(item) {
                item.status = !item.status;
            },
            removeTask(index) {
                this.todoList.splice(index, 1)
            },
            finishTask() {
                var fcount = 0
                this.todoList.forEach((item, index) => {
                    if (item.status == true) {
                        fcount++
                    }
                })
                return count;
            },
            undoTask() {
                var ucount = 0
                this.todoList.forEach((item, index) => {
                    if (item.status == false) {
                        ucount++
                    }
                })
                return ucount;
            }
        }
    })
</script>